package com.bleeth.fx.component;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.bleeth.fx.helper.StyleHelper;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import lombok.Builder;


/**
 * @author Sakura
 * @version 1.0
 * @date 2021-11-13 22:17
 **/
@Builder
public class MBadge {

    private Region region;

    private String value;

    private String color;


    private boolean hidden;


    public Region make() {
        if (StrUtil.isEmpty(value)) {
            value = "";
        }
        String labelValue = value;
        if (value.length() >= 4) {
            if (NumberUtil.isNumber(value)) {
                labelValue = StrUtil.sub(value, 0, 3) + "+";
            } else {
                labelValue = StrUtil.sub(value, 0, 3) + ".";
            }
        }
        Label label = new Label(labelValue);
        label.setPrefWidth(30);
        label.setPrefHeight(20);

        StackPane root = new StackPane();
        root.getChildren().addAll(region, label);
        StackPane.setAlignment(region, Pos.BOTTOM_LEFT);
        StackPane.setAlignment(label, Pos.TOP_RIGHT);
        label.setLayoutX(region.getPrefWidth());


        root.prefHeightProperty().bind(region.prefHeightProperty().add(10));
        root.prefWidthProperty().bind(region.prefWidthProperty().add(20));

        StyleHelper.bg(label).color(color).radius(label.getPrefHeight() / 2).make();
        label.setAlignment(Pos.CENTER);
        StyleHelper.ft(label).fill("#ffffff").make();
        return root;
    }


}
